<?php		


 
class MongoFace extends FaceBase{

	/**
	 * 构造函数，初始化插件
	 */
	public function __construct() {
		parent::__construct();
		n_api("manager")->isLogin();
		n_mod("skin")->header();
	}
	
	
	public function datapush(){
		$db = n_api("mongo")->db001;
		$fhandle = fopen("/opt/data/xaa.csv",'r');
		fseek($fhandle, rand(0, 190000000));
		fgets($fhandle);
		$content = mb_convert_encoding(fgets($fhandle), "utf-8",'gbk');
		print_r($content);
		$db->insert("big_data",array("content"=>addslashes($content)));
		
	}
	
	public function dataread(){
		$db = n_api("mongo")->db001;
		
		$ids = array();
		for ($i=0; $i < 100; $i++) {
			$ids[] = rand(1, 1800000);
		}
		echo microtime(true).'=';
		$rs = $db->get("big_data",$ids);
		echo microtime(true);
// print_r($rs);
	}	
	
	

	public function index(){
		$config = n_api('config');
		$mdb = $config->get('mdata');
 
		$mg = new Mongo("mongodb://{$mdb}");		

		$rs = $mg->listDBs();

		
		face('common','header');
		$this->_html('index',$rs);
		face('common','footer');
		
		
	}
	
	public function collections(){
		
		$param = $this->param('db:char');
		$mongo = new MyMongo($param['db']);
		$rs['db'] = $param['db'];
		$lc = $mongo->db()->listCollections();
		foreach($lc as $key=>$val){
			$rs['lc'][] = $val->getName();
		}
		
		face('common','header');
		$this->_html('collections',$rs);
		face('common','footer');
		
		
		
	} 
	public function getGridFS(){
			
		$param = $this->param('db:char');
		$mm = new MyMongo($param['db']);
		

		$mgfs = new MongoGridFS($mm->db());
		$lc = $mgfs->find();
		
		$rs['db'] = $param['db'];
		foreach($lc as $key=>$val){
			$item['uid'] = $val->file['uid'];
			$item['extend'] = $val->file['extend'];
			$item['ctime'] = $val->file['uploadDate']->sec;
			$item['size'] = $val->file['length'];
			$item['md5'] = $val->file['md5'];
			$item['id'] = (string)$val->file['_id'];
			$item['filename'] = $val->file['title'];
			$rs['list'][] = $item;
		}

		face('common','header');
		$this->_html('getGridFS',$rs);
		face('common','footer');
		
		
		
	}
	

	public function datalist(){
		$param = $this->param('db:char,cl:char');
		
		$rs = $param;
		$mongo = new MyMongo($param['db']);
				
		$fg = $mongo->collection($param['cl'])->find()->timeout(1000);
		
		foreach($fg as $key=>$val){
			unset($val['_id']);
			$rs['data'][$key] = $val;
			
		}
		
//		$mongo->insert($param['cl'],$rs);
		face('common','header');

		$this->_html('datalist',$rs);
		face('common','footer');
		
		
	}
	
	public function del(){
		
		$param = $this->param("db:char,cl:char,id:char");
		$mongo = new MyMongo($param['db']);
		$mongo->del($param['cl'],$param['id']);
		
	}
	
	
	
	
	
	public function upload(){

		$param = $this->param("db:char");
		face('common','header');
		$this->_html('upload',$param);
		face('common','footer');
		
	}
	/**
	 * 只能传name=file的，批量上传也仅仅是一个个file的传
	 */
	public function save(){

		$param = $this->param("db:char,filename:char:ignore");
		
		$mm = new MyMongo($param['db']);
		
		if(isset($param['filename'])){
			if(!file_exists($param['filename'])){
				throw new PException("找不到文件！");
			}
			$pinfo = pathinfo($param['filename']);
			$fi = finfo_open();
			print_r(finfo_file($fi,$param['filename']));
			$data['type'] = mime_content_type($param['filename']);
			$data['name'] = $pinfo['basename']; 
			$data['tmp_name'] = $param['filename'];
			$data['size'] = filesize($param['filename']);
			$data['extend'] = $pinfo['extension']; 
		
		}else{
		
			$com = n_api('common');
			$data = $com->upload('file');
		}
		$da = n_api("data");
		$rs = $da->save($data,$param['db']);
		
		header("location:".n_url('getGridFS')."&db={$param['db']}");	
		

		
	}
	
	
	public function getPic(){
		
		$param = $this->param('db:char,id:char');

		$mm = new MyMongo($param['db']);
		$mgfs = new MongoGridFS($mm->db());
		$rs = $mgfs->findOne(array('_id' => new MongoId($param['id'])));

		ob_end_clean();
		header("Content-Type: {$rs->file['mime']}");
		echo $rs->getBytes();
	}
	
	public function __destruct(){
		n_mod('skin')->footer();		
	}	
	
}